var table, params, $, form;
layui.use(['jquery','form','formSelects'], function(){
    $ = jQuery = layui.$;
    form = layui.form;
    var formSelects = layui.formSelects;

    formSelects.data('column_type', 'server', {
        url: ctx+'/sys_admin/sys/dict/getData',
        keyword: 'columntype',
        success: function(id, url, searchVal, result){
            formSelects.value('column_type', [column_type]);
            if (column_type == 'text' || column_type == 'longtext' || column_type == 'date' || column_type == 'datetime') {
                $('#lengthid').val('');
                $('#lengthid').attr('lay-verify', '');
                $('#lengthdivid').hide();
            } else {
                if (column_type == 'double') {
                    $('#decimalid').val(decimal);
                    $('#decimalid').attr('lay-verify', 'required|number');
                    $('#decimaldivid').show();
                }
            }
            if (column_type == 'select') {
                getForeignTables();
                getForeignKeys(foreignTable);
                $('#foreignTableid').val(foreignTable);
                $('#foreignKeyid').val(foreignKey);
                $('#foreignTableid').attr('lay-verify', 'required');
                $('#foreignKeyid').attr('lay-verify', 'required');
                $('#selectdivid').show();
                $('#selectkeydivid').show();
                $('#lengthdivid').hide();
                form.render();
            } else {
                $('#foreignTableid').val('');
                $('#foreignTableid').attr('lay-verify', '');
                $('#foreignKeyid').attr('lay-verify', '');
                $('#selectdivid').hide();
                $('#selectkeydivid').hide();
                $('#lengthdivid').show();
            }
        }
    });

    formSelects.on('column_type', function(id, vals, val, isAdd, isDisabled){
        $('#decimalid').val('');
        if (val.value == 'text' || val.value == 'longtext' || val.value == 'date' || val.value == 'datetime') {
            $('#lengthid').val('');
            $('#lengthid').attr('lay-verify', '');
            $('#lengthdivid').hide();
        } else {
            $('#lengthid').val('');
            $('#lengthid').attr('lay-verify', 'required|number');
            $('#lengthdivid').show();
            if (val.value == 'double') {
                $('#decimalid').attr('lay-verify', 'required|number');
                $('#decimaldivid').show();
            } else {
                $('#decimalid').attr('lay-verify', '');
                $('#decimaldivid').hide();
            }
            if (val.value == 'select') {
                getForeignTables();
                getForeignKeys(foreignTable);
                $('#foreignTableid').val(foreignTable);
                $('#foreignKeyid').val(foreignKey);
                $('#foreignTableid').attr('lay-verify', 'required');
                $('#foreignKeyid').attr('lay-verify', 'required');
                $('#lengthid').attr('lay-verify', '');
                $('#selectdivid').show();
                $('#selectkeydivid').show();
                $('#lengthdivid').hide();
                form.render();
            } else {
                $('#foreignTableid').val('');
                $('#foreignTableid').attr('lay-verify', '');
                $('#foreignKeyid').attr('lay-verify', '');
                $('#selectdivid').hide();
                $('#selectkeydivid').hide();
                $('#lengthdivid').show();
            }
        }
    });

   /*
   formSelects.on('foreignTable', function(id, vals, val, isAdd, isDisabled){
        getForeignKeys(val.value);
    });
    */
    form.on('select(foreignTable)', function(data){
        getForeignKeys(data.value);
    });
    form.on('select(foreignKey)', function(data){
        $('#foreignKeyid').val(data.value);
    });

    form.on('submit', function(data){
        $.post(ctx+'/sys_admin/modules/saveColumns', data.field, function(result) {
            if (result.success) {
                if (params.name == undefined) {
                    table.reload({});
                } else {
                    table.reload({
                        where:{
                            name : params.name,
                            status : params.status
                        },
                        page: {
                            curr: params.curr
                        }
                    });
                }
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            } else {
                layer.msg(result.msg);
            }
        }, 'json');
        return false;
    });

});

function getForeignTables() {
    $.ajax({
        type: "POST",
        url: ctx+'/sys_admin/modules/getForeignTables',
        data: {'moduleid': moduleid},
        async: false,
        success: function (data) {
            $("#foreignTableid").empty();
            $.each(data, function(i, module){
                $('#foreignTableid').append('<option value="'+module.id+'">'+module.name+'</option>>');
            });
        }
    });
}

function getForeignKeys(foreignmoduleid) {
    $.ajax({
        type: "POST",
        url: ctx+'/sys_admin/modules/getColumnsList',
        data: {'moduleid': foreignmoduleid, 'page':1, 'limit':100},
        async: false,
        success: function (data) {
            $("#foreignKeyid").empty()
            $.each(data.data, function(i, column){
                $('#foreignKeyid').append('<option value="'+column.column_name+'">'+column.column_comment+'</option>>');
                form.render();
            });
        }
    });
}